package club.justwill.single.persistence.jpo;

import club.justwill.single.persistence.support.SkusBase;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by luoyu on 15-12-24.
 */
@Entity
@Table(name = "skus")
@NamedQueries({
        @NamedQuery(name="Skus.getByProjectId",
                query="select sku from Skus sku where sku.projectId =:projectId order by sku.unitprice"),
        @NamedQuery(name="Skus.getBySkusId",
                query="select sku from Skus sku where sku.skusId = :skusId"),
        @NamedQuery(name="Skus.searchOrdersByProjectId",
                query="select sku from Skus sku where sku.projectId = :projectId"),
        @NamedQuery(name="Skus.searchSkuListBySkuIdList",
                query="select sku from Skus sku where sku.skusId in (:skuIdList)")
})
public class Skus extends SkusBase {

    @Id
    @Column(name = "sku_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long skusId;

    //一个项目包含多个sku sku跟项目是多对一关系 多方
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", referencedColumnName = "project_id", insertable = false, updatable = false)
    private Projects projects;

    //Skus跟Ordertem是一对多关系 一方
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "orders")
    @OrderBy("proc_ts desc")
    private Set<OrderItem> orderItemSet;

    public Set<OrderItem> getOrderItemSet() {
        if (orderItemSet==null){
            orderItemSet = new HashSet<OrderItem>();
        }
        return orderItemSet;
    }

    public void setOrderItemSet(Set<OrderItem> orderItemSet) {
        this.orderItemSet = orderItemSet;
    }

    public Projects getProjects() {
        return projects;
    }

    public void setProjects(Projects projects) {
        this.projects = projects;
    }

    public long getSkusId() {
        return skusId;
    }

    public void setSkusId(Long skusId) {
        if(skusId == null) {
            this.skusId = 0L;
        } else {
            this.skusId = skusId;
        }
    }
}
